package com.wondertek.poms.common.utils;

import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadPoolExecutor;

import com.wondertek.poms.common.constant.Constants;

/**
 * <p><b>DefaultRejectedHandler Description:</b> 线程池拒绝策略</p>
 * @author douzi
 * <b>DATE</b> 2020年4月14日 下午8:04:56
 */
public class DefaultRejectedHandler implements RejectedExecutionHandler {
	private int queueCapacity;
	
	public DefaultRejectedHandler(int queueCapacity) {
		this.queueCapacity = queueCapacity;
	}
	
    @Override
    public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
    	try {
            while (executor.getQueue().size() > queueCapacity * Constants.OVERFLOW_WAIT) {
                Thread.sleep(50L);
            }
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
        executor.submit(r);
    }
}
